﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp
{
    public class HttpConnectionPoolingSamples
    {
        public static async Task PoolingSamplesMain()
        {
            var ips = await Dns.GetHostAddressesAsync("www.baidu.com");
             
            foreach (var ipAddress in ips)
            {
                Console.WriteLine(ipAddress.MapToIPv4().ToString());
            }

            var socketsHandler = new SocketsHttpHandler
            {
                PooledConnectionLifetime = TimeSpan.FromMinutes(10),
                PooledConnectionIdleTimeout = TimeSpan.FromMinutes(5),
                MaxConnectionsPerServer = 10
            };

            var client = new HttpClient(socketsHandler);

            for (var i = 0; i < 5; i++)
            {
                _ = await client.GetAsync("https://www.baidu.com");
                await Task.Delay(TimeSpan.FromSeconds(2));
            }


            Console.WriteLine("Press a key to exit...");
            Console.ReadKey();
        }
        public static async Task PoolingSamplesMain2()
        {
            var ips = await Dns.GetHostAddressesAsync("www.baidu.com");


            foreach (var ipAddress in ips)
            {
                Console.WriteLine(ipAddress.MapToIPv4().ToString());
            }


            var socketsHandler = new SocketsHttpHandler
            {
                PooledConnectionLifetime = TimeSpan.FromSeconds(1),
                PooledConnectionIdleTimeout = TimeSpan.FromSeconds(1),
                MaxConnectionsPerServer = 10
            };


            var client = new HttpClient(socketsHandler);

            for (var i = 0; i < 5; i++)
            {
                _ = await client.GetAsync("https://www.baidu.com");
                await Task.Delay(TimeSpan.FromSeconds(2));
            }


            Console.WriteLine("Press a key to exit...");
            Console.ReadKey();
        }


        public static async Task PoolingSamplesMain3()
        {
            var ips = await Dns.GetHostAddressesAsync("www.baidu.com");

            foreach (var ipAddress in ips)
            {
                Console.WriteLine(ipAddress.MapToIPv4().ToString());
            }

            var socketsHandler = new SocketsHttpHandler
            {
                PooledConnectionLifetime = TimeSpan.FromSeconds(60),
                PooledConnectionIdleTimeout = TimeSpan.FromMinutes(20),
                MaxConnectionsPerServer = 2
            };

            var client = new HttpClient(socketsHandler);
            var sw = Stopwatch.StartNew();
            var tasks = Enumerable.Range(0, 200).Select(i => client.GetAsync("https://www.baidu.com"));
            await Task.WhenAll(tasks);
            sw.Stop();
            Console.WriteLine($"{sw.ElapsedMilliseconds}ms taken for 200 requests");
            Console.WriteLine("Press a key to exit...");
            Console.ReadKey();
        }


    }
}

 